<?php

namespace App\Service\Common;

use Exception;
use Illuminate\Support\Facades\Log;
use Kuafu\VoiceService\VoiceCloud;

class SendVoiceService
{

    /**
     * 向指定号码发送语音通知文件类型的语音通知
     * @param string $mobile 手机号
     * @param string $templateCode 模板ID
     * @throws Exception
     */
    public function sendCallByVoice(string $mobile, string $templateCode)
    {

        try {
            $res = (new VoiceCloud())::singleCallByVoice(
                [
                    "calledNumber" => $mobile,
                    "voiceCode"    => $templateCode,
                ]
            );
            Log::info("发送语音通知", ['res' => $res, 'mobile' => $mobile, 'templateCode' => $templateCode]);
            /*if ($res['Code'] == 'OK') {//成功

            } else {

            }*/
        } catch (Exception $exception) {
            throw new Exception($exception->getMessage());
        }
    }

    /**
     * 向指定号码发送语音验证码或带变量的语音通知
     * @param string $mobile 手机号
     * @param string $templateCode 模板ID
     * @param string $templateParam 模板中的变量参数
     * @throws Exception
     */
    public function sendVoiceCallByTts(string $mobile, string $templateCode, string $templateParam)
    {

        try {
            $res = (new VoiceCloud())::singleCallByTts(
                [
                    "calledNumber" => $mobile,                                   //接收语音通知的号码。
                    "ttsCode"      => $templateCode,                             //已通过审核的语音通知文本转语音模板或语音验证码模板的模板ID
                    "ttsParam"     => $templateParam,                            //模板中的变量参数，JSON格式。{"AckNum":"123456"}
                ]
            );
            Log::info("发送语音通知带参数", ['res' => $res, 'mobile' => $mobile, 'templateCode' => $templateCode, 'templateParam' => $templateParam]);
            /*if ($res['Code'] == 'OK') {//成功

            } else {

            }*/
        } catch (Exception $exception) {
            throw new Exception($exception->getMessage());
        }
    }
}
